import 'package:flutter_admin/api/odoo/purchase_evaluation_item_odoo_api.dart';
import 'package:flutter_admin/models/odoo/purchase_evaluation_item.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class PurchaseEvaluationItemEdit extends StatefulWidget {
  var templateName = '';
  var deptName = '';
  var projectName = '';
  var projectId = '';
  PurchaseEvaluationItemEdit({
    this.purchaseEvaluationItem,
    required this.templateName,
    required this.deptName,
    required this.projectName,
    required this.projectId,
  });

  final PurchaseEvaluationItem? purchaseEvaluationItem;

  @override
  _PurchaseEvaluationItemEditState createState() =>
      _PurchaseEvaluationItemEditState();
}

class _PurchaseEvaluationItemEditState
    extends State<PurchaseEvaluationItemEdit> {
  late PurchaseEvaluationItem purchaseEvaluationItem;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.purchaseEvaluationItem == null;
    this.purchaseEvaluationItem =
        widget.purchaseEvaluationItem ?? PurchaseEvaluationItem();
    purchaseEvaluationItem.templateName = widget.templateName;
    purchaseEvaluationItem.deptName = widget.deptName;
    purchaseEvaluationItem.projectName = widget.projectName;
    purchaseEvaluationItem.projectId = widget.projectId;
    _loadSelectVO();
  }

  _loadSelectVO() async {
    // templateList = await DynamicDataApi.getModelSelect(
    //   model: "purchase.evaluation.template",
    //   name: "template_name",
    //   active: false,
    // );
    // projectList = await DynamicDataApi.getModelSelect(
    //   model: "purchase.evaluation.project",
    //   name: "evaluation_project",
    //   active: false,
    // );
    // deptIdList = await DynamicDataApi.getModelSelect(
    //   model: "hr.department",
    //   name: "name",
    // );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            width: 280,
            label: '模板名称',
            enable: false,
            value: purchaseEvaluationItem.templateName,
            onSaved: (v) {
              purchaseEvaluationItem.templateName = v;
            },
          ),
          CryInput(
            width: 280,
            label: '考评项目',
            enable: false,
            value: purchaseEvaluationItem.projectName,
            onSaved: (v) {
              purchaseEvaluationItem.projectName = v;
            },
          ),
          CryInput(
            width: 280,
            label: '评价部门',
            enable: false,
            value: purchaseEvaluationItem.deptName,
            onSaved: (v) {
              purchaseEvaluationItem.deptName = v;
            },
          ),
          CryInput(
            width: 280,
            label: '考核内容',
            value: purchaseEvaluationItem.assessmentContent,
            onSaved: (v) {
              purchaseEvaluationItem.assessmentContent = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            width: 280,
            label: '权重分数',
            value: purchaseEvaluationItem.weightScore,
            onSaved: (v) {
              purchaseEvaluationItem.weightScore = v;
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '权重分数不能为0'
                      : int.parse(v) > 100
                          ? '权重分数小于等于100'
                          : null;
            },
          ),
          CryInput(
            width: 280,
            label: '评估等级标准',
            value: purchaseEvaluationItem.evaluationCriteria,
            onSaved: (v) {
              purchaseEvaluationItem.evaluationCriteria = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            PurchaseEvaluationItemOdooApi.saveOrUpdate(
                    purchaseEvaluationItem.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
            // });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
